package com.example.himalayaproject.find.broadcast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.adapter.CommonAdapter;
import com.example.himalayaproject.find.broadcast.bean.LocalEntity;
import com.example.himalayaproject.find.broadcast.callback.BroadcastHttpService;
import com.example.himalayaproject.find.broadcast.callback.OnPageLoadListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class CommonActivity extends AppCompatActivity implements Callback<LocalEntity>, View.OnClickListener, OnPageLoadListener {

    private BroadcastHttpService service;
    private RecyclerView recycler;
    private CommonAdapter adapter;
    private TextView typeTextView;
    private Intent intent;
    private String type;
    private int TAG;
    private int pageNum = 1;
    private ImageView backImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);

        intent = getIntent();
        type = intent.getStringExtra("type");
        TAG = intent.getIntExtra("TAG",1);
        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BroadcastHttpService.class);

        switch (TAG) {
            case 1:
                service.getResult(pageNum,20,"370000",2).enqueue(this);
                break;
            case 2:
                service.getResult(pageNum,20,null,1).enqueue(this);
                break;
            case 4:
                service.getResult(pageNum,20,null,3).enqueue(this);
                break;
        }

        init();
    }

    private void init() {
        recycler = (RecyclerView) findViewById(R.id.bct_common_recycler);
        typeTextView = (TextView) findViewById(R.id.bct_common_type);
        backImageView = (ImageView) findViewById(R.id.bct_common_back);
        adapter = new CommonAdapter(this,this);
        recycler.setAdapter(adapter);
        typeTextView.setText(type);
        backImageView.setOnClickListener(this);
    }

    @Override
    public void onResponse(Call<LocalEntity> call, Response<LocalEntity> response) {
        if (response.body().getResult()!=null){
            adapter.addAll(response.body().getResult());
        }
    }

    @Override
    public void onFailure(Call<LocalEntity> call, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bct_common_back:
                finish();
                break;
        }
    }

    @Override
    public void onPageLoad() {
        switch (TAG) {
            case 1:
                pageNum++;
                service.getResult(pageNum,20,"370000",2).enqueue(this);
                break;
            case 2:
                pageNum++;
                service.getResult(pageNum,20,null,1).enqueue(this);
                break;
            case 4:
                pageNum++;
                service.getResult(pageNum,20,null,3).enqueue(this);
                break;
        }
    }
}
